package com.ysstech.valuation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.util.BigDecimalUtil;
import com.ysstech.valuation.entity.FuturesDealDetailed;
import com.ysstech.valuation.entity.SecuritiesDealDetailed;
import com.ysstech.valuation.entity.StockDealDetailed;
import com.ysstech.valuation.mapper.FuturesDealDetailedMapper;
import com.ysstech.valuation.service.FuturesDealDetailedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 期货交易明细表 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2021-07-05
 */
@Slf4j
@Service
public class FuturesDealDetailedServiceImpl extends ServiceImpl<FuturesDealDetailedMapper, FuturesDealDetailed> implements FuturesDealDetailedService {

    /**
     * 获取期货交易的明细数据，返回对象封装成股票的 没笔交易的成交金额
     *
     * @param futuresDealDetailed
     * @return
     * @throws Exception
     */
    public Map<String, List<SecuritiesDealDetailed>> queryFuturesDealDetailed(FuturesDealDetailed futuresDealDetailed) throws Exception {
        Map<String, List<SecuritiesDealDetailed>> map = new LinkedHashMap<>();
        if (null == futuresDealDetailed || StringUtils.isBlank(futuresDealDetailed.getAccountId())
                || null == futuresDealDetailed.getTransactionDate()) {
            log.info("没有获取到明细交易数据！");
            return map;
        }
        QueryWrapper<FuturesDealDetailed> wrapper = new QueryWrapper<>();
        wrapper.eq("ACCOUNT_ID", futuresDealDetailed.getAccountId());
        wrapper.eq("TRANSACTION_DATE", futuresDealDetailed.getTransactionDate());
        wrapper.exists("SELECT 1 FROM etf_account a WHERE CLEAR_CODE = 'N' and a.SECURITY_CODE=TRADING_CODE");
        wrapper.orderByAsc("TRANSACTION_NUMBER");
        List<FuturesDealDetailed> list = this.list(wrapper);
        String purchase = "";
        for (FuturesDealDetailed dealDetailed : list) {
            if (null == dealDetailed) {
                continue;
            }
            List<SecuritiesDealDetailed> detaileds = new ArrayList<>();
            SecuritiesDealDetailed securitiesDealDetailed = new SecuritiesDealDetailed();
            if ("B".equals(dealDetailed.getBusinessType())) { //申购
                BigDecimal dealMoney = BigDecimalUtil.add(dealDetailed.getDealMoney(), dealDetailed.getProceduresFee());
                securitiesDealDetailed.setDealMoney(dealMoney);
                purchase = "SG";
            }
            if ("S".equals(dealDetailed.getBusinessType())) { //赎回
                BigDecimal dealMoney = BigDecimalUtil.subtract(dealDetailed.getDealMoney(), dealDetailed.getProceduresFee());
                securitiesDealDetailed.setDealMoney(dealMoney);
                purchase = "SH";
            }
            if (StringUtils.isBlank(purchase)) {
                continue;
            }
            securitiesDealDetailed.setDealAmount(dealDetailed.getDealAmount());
            String key = dealDetailed.getTradingCode() + "\t" + purchase;
            if (map.containsKey(key)) {
                detaileds = map.get(key);
                detaileds.add(securitiesDealDetailed);
                map.put(key, detaileds);
            } else {
                detaileds.add(securitiesDealDetailed);
                map.put(key, detaileds);
            }
        }
        return map;
    }
}
